package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.Order;
import com.coolwen.entity.User;
import com.coolwen.service.GroupService;
import com.coolwen.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Eoly on 2016/12/7.
 */
@Controller
@SessionAttributes("loginUser")
@RequestMapping("/")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    GroupService groupService;

    /**
     * 查询用户所有的订单列表
     *
     * @param loginUser
     * @param model
     * @return
     */
    @RequestMapping("login-user/my-order-list")
    public String showOderList(@ModelAttribute("loginUser") User loginUser, Model model) {
        model.addAttribute("orderList", orderService.findOrderByUserId(loginUser.getUserId()));
        return "/web/Order";
    }

    /**
     * 得到用户订单通过Json数据
     * @param loginUser
     * @return
     */
    @ResponseBody
    @RequestMapping("login-user/check-order-list")
    public String checkOderList(@ModelAttribute("loginUser") User loginUser) {
        List<Order> orders = null;
        orders = orderService.findOrderByUserId(loginUser.getUserId());
        return JSON.toJSON(orders).toString();
    }

    /**
     * 订单付款
     * @param loginUser
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login-user/order-set", produces = "text/html;charset=UTF-8")
    public String changeOrderState(@ModelAttribute("loginUser") User loginUser, @RequestParam Integer orderId) {
        Order order = new Order();
        order.setOrderState(2);
        order.setOrderId(orderId);
        orderService.updateOrderByOrderId(order);
        return "付款成功";
    }


}
